﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SysRouteLinq.Forms
{
    public partial class ConsultaEmpresa : Form
    {
        private SysRouteDataContext dc;
        private bool result = false;

        public ConsultaEmpresa()
        {
            InitializeComponent();
            dc = new SysRouteDataContext();
        }

        private void ConsultaEmpresa_Shown(object sender, EventArgs e)
        {

            fillComboBox();
        }

        private void fillComboBox()
        {
            IEnumerable<Empresa> r = from f in dc.Empresas
                                     orderby
                                         f.nome
                                     select f;
            comboBox1.DataSource = null;
            comboBox1.DataSource = r.ToList();
            comboBox1.DisplayMember = "nome";
            comboBox1.ValueMember = "id_empresa";

            Empresa r2 = (from f in dc.Empresas
                          where f == comboBox1.SelectedItem
                          select f).Single();

            textBox2.Text = r2.CNPJ;
            textBox3.Text = r2.endereco;
            textBox4.Text = r2.telefone;

            result = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Empresa r = (from f in dc.Empresas
                            where
                            f == comboBox1.SelectedItem
                            select f).Single();

            EditEmpresa form = new EditEmpresa(r.id_empresa);
            if (form.ShowDialog() == DialogResult.OK)
            {
                dc = new SysRouteDataContext();
                fillComboBox();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {

            Empresa emp = (from f in dc.Empresas
                        where
                        f == comboBox1.SelectedItem
                        select f).Single();

            //MessageBox.Show("Deseja realmente excluir?");
            
            
            IEnumerable<Linha> l = (from f2 in dc.Linhas
                                          where
                                             f2.id_empresa == emp.id_empresa
                                          select f2);

            foreach (Linha i in l)
            {
                i.id_empresa = null;
                dc.SubmitChanges();
            }

            dc.Empresas.DeleteOnSubmit(emp);
            dc.SubmitChanges();
        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (result == true)
            {
                var l = from f in dc.Empresas
                             where f == comboBox1.SelectedItem
                             select f;
                if (l.Count() == 0)
                {
                    textBox2.Clear();
                    textBox3.Clear();
                    textBox4.Clear();
                }
                else
                {
                    Empresa r = (from f in dc.Empresas
                                 where f == comboBox1.SelectedItem
                                 select f).Single();

                    textBox2.Text = r.CNPJ;
                    textBox3.Text = r.endereco;
                    textBox4.Text = r.telefone;
                }
            }
            else { }
        }
    }
}
